package com.ruoyi.teaching.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.teaching.dto.MyPlanDTO;
import com.ruoyi.teaching.dto.PreviewTeachingPlanDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.teaching.domain.YaTeachingPlan;
import com.ruoyi.teaching.service.IYaTeachingPlanService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 教学计划(课)Controller
 * 
 * @author ruoyi
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/teaching_plan")
public class YaTeachingPlanController extends BaseController
{
    @Autowired
    private IYaTeachingPlanService yaTeachingPlanService;

    /**
     * 查询已有教学计划(课)列表
     */
    @RequiresPermissions("teaching:teaching_plan:list")
    @GetMapping("/list/all")
    public TableDataInfo listAll(YaTeachingPlan yaTeachingPlan)
    {
        List<YaTeachingPlan> list = yaTeachingPlanService.selectYaTeachingPlanList(yaTeachingPlan);
        return getDataTable(list);
    }

    /**
     * 生成预览教学计划(课)
     */
    @RequiresPermissions("teaching:teaching_plan:add")
    @Log(title = "教学计划(课)", businessType = BusinessType.INSERT)
    @PostMapping("/preview")
    public TableDataInfo preview(@Valid @RequestBody PreviewTeachingPlanDTO previewTeachingPlanDTO, Errors errors)
    {
        //表单校验
        if (errors.hasErrors()) {
            List<ObjectError> allErrors = errors.getAllErrors();
            StringBuilder errMsg = new StringBuilder();
            for (ObjectError error : allErrors) {
                errMsg.append(error.getDefaultMessage());
                errMsg.append(";");
            }
            throw new ServiceException(errMsg.toString());
        }
        return getDataTable(yaTeachingPlanService.previewTeachingPlan(previewTeachingPlanDTO));
    }

    /**
     * 调为上课
     */
    @RequiresPermissions("teaching:teaching_plan:add")
    @Log(title = "教学计划(课)", businessType = BusinessType.INSERT)
    @PostMapping("/workday")
    public AjaxResult addWorkday(@Valid @RequestBody PreviewTeachingPlanDTO previewTeachingPlanDTO)
    {
        yaTeachingPlanService.addWorkday(previewTeachingPlanDTO);
        return AjaxResult.success(yaTeachingPlanService.previewTeachingPlan(previewTeachingPlanDTO));
    }

    /**
     * 调为休息
     */
    @RequiresPermissions("teaching:teaching_plan:add")
    @Log(title = "教学计划(课)", businessType = BusinessType.INSERT)
    @PostMapping("/holiday")
    public AjaxResult addHoliday(@Valid @RequestBody PreviewTeachingPlanDTO previewTeachingPlanDTO)
    {
        yaTeachingPlanService.addHoliday(previewTeachingPlanDTO);
        return AjaxResult.success(yaTeachingPlanService.previewTeachingPlan(previewTeachingPlanDTO));
    }

    /**
     * 清除对应节假日配置
     */
    @RequiresPermissions("teaching:teaching_plan:list")
    @GetMapping("/clearConfig")
    public TableDataInfo clearConfig(PreviewTeachingPlanDTO previewTeachingPlanDTO)
    {
        yaTeachingPlanService.clearConfig(previewTeachingPlanDTO);
        return getDataTable(yaTeachingPlanService.previewTeachingPlan(previewTeachingPlanDTO));
    }

    /**
     * 导出课程列表（从mysql到ES的数据全量同步）
     */
    @RequiresPermissions("teaching:teaching_plan:export")
    @Log(title = "高频面试题", businessType = BusinessType.EXPORT)
    @PutMapping("/exportEs")
    public AjaxResult exportEs()
    {
        yaTeachingPlanService.exportEs();
        return AjaxResult.success();
    }

    /**
     * 查询我的课表
     */
    @RequiresPermissions("teaching:plan:curriculum")
    @PostMapping("/myPlanList")
    public TableDataInfo myPlanList(@RequestBody MyPlanDTO myPlanDTO)
    {
        List<YaTeachingPlan> list = yaTeachingPlanService.selectMyPlanList(myPlanDTO);
        return getDataTable(list);
    }





    /**
     * 查询教学计划(课)列表
     */
    @RequiresPermissions("teaching:teaching_plan:list")
    @GetMapping("/list")
    public TableDataInfo list(YaTeachingPlan yaTeachingPlan)
    {
        startPage();
        List<YaTeachingPlan> list = yaTeachingPlanService.selectYaTeachingPlanList(yaTeachingPlan);
        return getDataTable(list);
    }

    /**
     * 导出教学计划(课)列表
     */
    @RequiresPermissions("teaching:teaching_plan:export")
    @Log(title = "教学计划(课)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YaTeachingPlan yaTeachingPlan)
    {
        List<YaTeachingPlan> list = yaTeachingPlanService.selectYaTeachingPlanList(yaTeachingPlan);
        ExcelUtil<YaTeachingPlan> util = new ExcelUtil<YaTeachingPlan>(YaTeachingPlan.class);
        util.exportExcel(response, list, "教学计划(课)数据");
    }

    /**
     * 获取教学计划(课)详细信息
     */
    @RequiresPermissions("teaching:teaching_plan:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yaTeachingPlanService.selectYaTeachingPlanById(id));
    }

    /**
     * 新增教学计划(课)
     */
    @RequiresPermissions("teaching:teaching_plan:add")
    @Log(title = "教学计划(课)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<YaTeachingPlan> yaTeachingPlanList)
    {
        return toAjax(yaTeachingPlanService.batchInsertYaTeachingPlan(yaTeachingPlanList));
    }

    /**
     * 修改教学计划(课)
     */
    @RequiresPermissions("teaching:teaching_plan:edit")
    @Log(title = "教学计划(课)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody List<YaTeachingPlan> yaTeachingPlanList)
    {
        return toAjax(yaTeachingPlanService.batchUpdateYaTeachingPlan(yaTeachingPlanList));
    }

    /**
     * 删除教学计划(课表)
     */
    @RequiresPermissions("teaching:teaching_plan:remove")
    @Log(title = "教学计划(课表)", businessType = BusinessType.DELETE)
    @DeleteMapping
    public AjaxResult remove(YaTeachingPlan yaTeachingPlan)
    {
        return toAjax(yaTeachingPlanService.deleteTeachingPlan(yaTeachingPlan));
    }
}
